/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import main.WinkelApplication;
import model.Klant;



/**
 *
 * @author Jack Sierkstra
 */
public class CustomerEdit extends JPanel{
    
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    
    TextField naamtext;
    TextField adrestext;
    TextField postcodetext;
    TextField woonplaatstext;
    
    public CustomerEdit(int klantid) {
        
      super();
      setLayout(new BorderLayout());
      addTitle();
      editpanel(klantid);
      addButtonPane(klantid);
    }
    
    
    public void addTitle() {
        JLabel titel = new JLabel("Klant wijzigen", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.getInstance().computeFontSize(2));
        
        JPanel frame = new JPanel();
        frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));

        frame.add(titel);
        
        titel.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        add(frame, BorderLayout.PAGE_START);
    }
    
    public void addButtonPane(final int klantid) {
           
        JPanel frame = new JPanel();
        frame.setPreferredSize(new Dimension(dim.width, 40));
        
        JButton opslaan = new JButton("Wijzigingen opslaan");
        
        opslaan.setPreferredSize(new Dimension(150,25));
        opslaan.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             
              saveEdit(klantid, naamtext.getText(), adrestext.getText(), postcodetext.getText(), woonplaatstext.getText());
              
              
          }
        });
        
        JButton terug = new JButton("Annuleren");
        
        terug.setPreferredSize(new Dimension(150,25));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             
             javax.swing.SwingUtilities.getWindowAncestor(CustomerEdit.this).dispose();
          }
        });

        
        frame.add(opslaan);
        frame.add(terug);
        
        add(frame, BorderLayout.PAGE_END);
         
    }

    public void editpanel(final int klantid) {
        
        Klant klant = WinkelApplication.getQueryManager().getKlant(klantid);
            
           FormLayout layout = new FormLayout(
                "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
              + "right:max(40dlu;pref), 3dlu, 70dlu",
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p");
                
        JPanel panel = new JPanel(layout);
        panel.setBorder(Borders.DIALOG_BORDER);
       

        naamtext = new TextField();
        adrestext = new TextField();
        postcodetext = new TextField();
        woonplaatstext = new TextField();
        
        naamtext.setText(klant.getNaam());
        adrestext.setText(klant.getAdres());
        postcodetext.setText(klant.getPostcode());
        woonplaatstext.setText(klant.getWoonplaats());
        
        JLabel naam = new JLabel("Naam:");
        JLabel adres = new JLabel("Adres:");
        JLabel postcode = new JLabel("Postcode:");
        JLabel woonplaats = new JLabel("Woonplaats:");
        
        naam.setFont(WinkelApplication.computeFontSize(3));
        adres.setFont(WinkelApplication.computeFontSize(3));
        postcode.setFont(WinkelApplication.computeFontSize(3));
        woonplaats.setFont(WinkelApplication.computeFontSize(3));
        
        
        // Fill the table with labels and components.
        CellConstraints cc = new CellConstraints();
        
        panel.add(naam,            cc.xy (1,  3));
        panel.add(naamtext,                 cc.xyw(3,  3, 5));
        panel.add(adres,            cc.xy (1,  5));
        panel.add(adrestext,               cc.xyw(3,  5, 5));
        panel.add(postcode,           cc.xy (1, 7));
        panel.add(postcodetext,                     cc.xyw(3,  7, 5));
        panel.add(woonplaats,           cc.xy (1, 9));
        panel.add(woonplaatstext,                     cc.xyw(3,  9, 5));
  
         
          
        add(panel, BorderLayout.CENTER);
        
    }


    public void saveEdit(int klantid, String naam, String adres, String postcode, String woonplaats) {
       
        Klant klant = new Klant(klantid, naam, adres, postcode, woonplaats);
        
        WinkelApplication.getQueryManager().editCostumer(klant);
        
        WinkelApplication.getInstance().showPanel(new view.CustomerPanel());
        javax.swing.SwingUtilities.getWindowAncestor(CustomerEdit.this).dispose();
    }
    
    
 
}
